import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;


public class Ex4_2 {
	
	public Ex4_2() {}
	
	public static void main(String[] args) {
		Ex4_2 exe = new Ex4_2();
		ArrayList<String> dnas = 
							exe.getDNAs("C:\\Users\\josemfer\\Downloads\\DNAs.txt");
		exe.overlap(dnas);
	}
	
	
	private void overlap(ArrayList<String>  dnas) {
		
		HashMap<String, String> hm = new HashMap<String, String>();		
		String result = "";
		for (int i=0;i<dnas.size();i++) {
			String dna = dnas.get(i);
			String prefix = dna.substring(0,dna.length()-1);
			String sufix = dna.substring(1,dna.length());
			for (int j=i;j<dnas.size();j++) {
				String dna2 = dnas.get(j);
				String prefix2 = dna2.substring(0,dna.length()-1);
				String sufix2 = dna2.substring(1,dna.length());
				
				if (prefix2.equals(sufix)) {
					hm.put(dna, dna2);
					//System.out.println(dna +" -> " + dna2);
					 result += dna +" -> " + dna2 + "\n";
				}
				if (sufix2.equals(prefix)) {
					hm.put(dna2,dna);
					//System.out.println(dna2 + " -> " + dna);
					 result += dna2 +" -> " + dna + "\n";
				}				
				
			}
		}
		writeFile("C:\\Users\\josemfer\\Downloads\\ex4_2_result.txt",result);
		
		
	}
	
	
	
	
	private void writeFile(String path, String text) {
		try
		{
			String file_name = path;
			FileWriter file = new FileWriter(file_name);
			BufferedWriter out = new BufferedWriter (file);			
			out.write(text);
			out.close();
		}
		catch (IOException e)
		{}
	}
	
	private ArrayList<String> getDNAs(String file) {		
		ArrayList<String> lineas = new ArrayList<String>();		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				lineas.add(line);
			}
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return lineas;		
	}
	

}
